<template>
  <div>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onselect"
    />
  </div>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import axios from "axios";
import { Toast } from "vant";

@Component
export default class AddressList extends Vue {
  private chosenAddressId = 0;
  private list = [];

  protected created() {
    axios.get("http://localhost:8081/address/list").then((res) => {
      this.list = res.data.data;
    });
  }

  protected onAdd() {
    this.$router.push("/addressNew");
  }
  protected onEdit(item: object, index: number) {
    let data = JSON.stringify(item);
    this.$router.push({ path: "/addressEdit", query: { item: data } });
  }

  protected onselect(item: any, index: number) {
    let orderForm = {
      name: item.name,
      tel: item.tel,
      address: item.address,
      specsId: this.$store.state.specsId,
      quantity: this.$store.state.quantity,
    };
    axios.post("http://localhost:8081/order/create", orderForm).then((res) => {
      if (res.data.code == 0) {
        let instance = Toast("下单成功");
        setTimeout(() => {
          instance.close();
          this.$router.push("/detail?orderId=" + res.data.data.orderId);
        }, 1000);
      }
    });
  }
}
</script>
